/*----------------------------------------------
| noka input v1.0 www.97521.com                 |
| rebin 2014-01-20                             |
|---------------------------------------------*/
var ninput = Class.create({
    version: '1.0',
    initialize : function(cfg) {
    	this.cfg = cfg;
    	this.id=cfg.id; //id
    	this.rurl=cfg.rurl;
    	this.icosrc=cfg.rurl+'/nokatag/formcheck/images/exclamation.gif';
    	this.input = $(cfg.id);
    	this.allownull=cfg.allownull;//是否允许为空
    	this.ico=$(cfg.id+'_img');
    	this.div=$(cfg.id+'_div');
    	this.maxDiv = ($(cfg.id+'_maxDiv')==undefined?undefined:$(cfg.id+'_maxDiv'));
    	this.chtype=cfg.chtype;//验证内型chtype js|server|regex
    	this.verification=cfg.verification;//验证内容
    	this.veriSuccess = cfg.allownull;
    	this.ajaxchtype = false;// 是否正在执行ajva验证
    	this.pva = cfg.pva;//页面参数 ['aaa','ccc']
    	this.maxclass = cfg.maxclass;
    	this.showpass = (cfg.showpass == undefined?false:cfg.showpass);
    	this.isshow = false;//是否显示
    	this.onShowMax = cfg.onShowMax;
    	this.dfmt = cfg.dfmt;//格化日期
    	this.disabled = false;
    	this.readonly = false;
    },
    initinput : function(){
    	this.initPassInput();
    	this.initIco();
    	this.initMsg();
    	this.initMaxMsg();
    	this.InputVerEvent();
    	this.msgShowHide();
    	this.initObjCht();
    	this.onChangeColor();
    },
    //---------初始化input----------------
    initPassInput : function(){
    	var self = this;
    	var inputstr = $(self.id+'_intDiv');
    	var inputValue = ($(self.id)==undefined?'':$(self.id).value);
    	try{
	    	if(inputstr!=undefined){
		    	if(self.isshow){
		    		$(self.id).writeAttribute({type:'text'});
		    	}else{
		    		$(self.id).writeAttribute({type:'password'});
		    	}
		    	self.input = $(self.id);
		    	self.input.value=inputValue;
		    	self.InputVerEvent();
		    	self.initMaxMsg();
		    	self.PassWordButton();
		    	var chbutton = $(self.id+'_chdiv');
		    	if(chbutton!=undefined && self.showpass){
			    	if(self.isshow){
			    		chbutton.innerHTML='ON';
			    		//chbutton.setStyle({backgroundColor:'#76c61d'});
			    		chbutton.removeClassName('nk_input_passoff');
			    		chbutton.addClassName('nk_input_passno');
			    	}else{
			    		chbutton.innerHTML='OFF';
			    		//chbutton.setStyle({backgroundColor:'#14bbf9'});
			    		chbutton.removeClassName('nk_input_passno');
			    		chbutton.addClassName('nk_input_passoff');
			    	}
		    	}
	    	}
    	}catch(e){}
    	//-----------------格式化日期--------------------------------------
		try{
    		if(undefined != self.dfmt && self.dfmt.trim().length>0){
    			$(self.id).value = self.formdates($(self.id).value, self.dfmt);
    		}
		}catch(e){}
		//----------------------------------------------------------------
    },
    //---------初始化ico------------------
    initIco : function(){
    	try{
    		this.ico.src = this.icosrc;
    		this.ico.setStyle({verticalAlign:'middle'});
    		this.ico.hide();
    		this.input.setStyle({verticalAlign:'middle'});
    	}catch(e){}
    },
    //---------初始化msgdiv--------------
    initMsg : function(){
    	this.div.setStyle({display:'block',background:'#FFFFFF',border:'solid 1px #FF0000',zIndex:'9999',padding:'5px 5px 5px'});
    	this.div.absolutize();
    	if(this.maxDiv!=undefined){
    		this.maxDiv.innerHTML='&nbsp;';
    	}
    	this.div.hide();
    },
    //--------初始化放大层--------------
    initMaxMsg : function(){
    	var inobj = this.input;
    	if(this.maxDiv!=undefined){
	    	this.maxDiv.absolutize();
	    	this.maxDiv.setStyle({display:'block',background:'#FFFFFF',border:'solid 1px #FF0000',zIndex:'9999',padding:'5px 5px 5px'});
	    	this.maxDiv.style.top=inobj.positionedOffset().top-this.maxDiv.offsetHeight-5;//
	    	this.maxDiv.style.left=inobj.positionedOffset().left-2;//+inobj.offsetWidth;
	    	this.maxDiv.hide();
    	}
    },
    //---------初始化放大效果---------------
    showMax : function(v){
        if(this.maxDiv!=undefined){
	    	if(v.trim().length>0){
	    		if(this.onShowMax!=undefined){
	    			this.maxDiv.show();
	    	    	this.maxDiv.innerHTML=this.onShowMax(v);
	    		}else{
	    			this.maxDiv.show();
	    	    	this.maxDiv.innerHTML=v;
	    		}
	    	}else{
	    		this.maxDiv.hide();
	    	}
        }
    },
    //---------初始化密码显示按钮----------------
    PassWordButton:function(){
    	var self = this;
    	var idx = 0;
    	var html=[];
    	if(self.showpass){
    		html[idx++]='<div id="'+self.id+'_chdiv" style="float: left;" class="nk_input_lineheight nk_input_passoff" style="width: 30px">OFF</div>';
    	}else{
    		html[idx++]='<div id="'+self.id+'_chdiv" style="float: left;" class="nk_input_lineheight nk_input_passno"></div>';
    	}
    	if($(self.id+'_chbut')!=undefined){
	    	$(self.id+'_chbut').innerHTML=html.join('');
	    	var bwidth = $(self.id+'_chbut').getWidth();
	    	var inputWidth = $(self.id+'_sdDiv').getWidth()-bwidth-4;
	    	self.input.setStyle({width:inputWidth+'px'});
	    	self.input.width=inputWidth;
	    	$(self.id+'_chdiv').observe('click',function(){//失失焦点时验证其值
	    		if(!self.disabled && !self.readonly){
	    			self.isshow = !self.isshow;
	    			self.initPassInput();
	    		}
	     	 });
    	}
    	//-----------------------------------
    	if(!self.showpass){
    		$(self.id+'_chbut').hide();
    		var width = $(self.id+'_sdDiv').getWidth()-2;
    		$(self.id+'_intDiv').setStyle({width:width+'px'});
    		$(self.id).setStyle({width:width+'px'});
    	}else{
    		$(self.id+'_chbut').show();
    	}
    },
    //--------初始化验证-----------------
    initObjCht : function(){
    	var self = this;
    	$(self.id).veri = function(){
    		var val = $(self.id).value;
    			self.chVeris(val);//验证
    		if(!self.veriSuccess)
    			self.ico.show();
	    	return self.veriSuccess;
    	};
    	$(self.id).setValue = function(value,ch){
    		$(self.id).value = value;//设置值
    		//-----------------格式化日期--------------------------------------
    		try{
	    		if(undefined != self.dfmt && self.dfmt.trim().length>0){
	    			$(self.id).value = self.formdates(value, self.dfmt);
	    		}
    		}catch(e){}
    		//----------------------------------------------------------------
    		if(typeof ch == 'undefined'){
    			self.chVeris(value);//验证
    		}else{
    			self.veriSuccess=ch;
    			if(!ch){
    				$(self.id+'_img').show();
    			}else if(ch){
    				$(self.id+'_img').hide();
    			}
    				
    		}
    	};
    	//------------------------------------
    	$(self.id).isServer = function(){//是否为服务器验证
    		return 'server'==self.chtype;
    	};
    	//-----------------------------------
    	$(self.id).isVeriEnd = function(){//是否验证结束
    		return !self.ajaxchtype;//为false表示验证结束
    	};
    	//----------------------------------
    	$(self.id).sVerEnd = function(){//服务器验证结果
    		return  self.veriSuccess;
    	};
    	//----------------------------------
    	$(self.id).setDisabled = function(a){
    		$(self.id).writeAttribute({disabled:a});
    		self.disabled = a;
    	};
    	$(self.id).setReadonly = function(a){
    		$(self.id).writeAttribute({readonly:a});
    		self.readonly = a;
    	};
    },
    //---------增加验证事件---------------
    InputVerEvent : function(){
    	 var self = this;
    	 document.observe('widget:noka_reinit',function(event){//失失焦点时验证其值
    		 if(self.id.indexOf('_nkADinsert_')==-1){
	    		var npfffs=event.memo.widgetNumber.clone();//[{}]
	    		for(var i=0;i<npfffs.length;i++){
	    			var cfgid = Object.allclone(self.cfg);
	    			var initem = npfffs[i];
	    			cfgid.id = initem.id;
	    			if(cfgid.id.indexOf('_'+self.id+'_')!=-1){
	    				(new ninput(cfgid)).initinput();
	    				try{
	    					var idx = cfgid.id.substring(cfgid.id.lastIndexOf('_')+1,cfgid.id.length);
	    					$(cfgid.id).name=$(cfgid.id).name.replace(/\[0\]/g,'['+idx+']');
	    					if(undefined!=initem.value && $(cfgid.id).name==initem.name){
	    						$(cfgid.id).setValue(initem.value);
	    					}
	    				}catch(e){}
	    			}
	    		}
    		 }
    	 });
    	 self.input.observe('blur',function(){//失失焦点时验证其值
    		self.chVeris(this.value);
    	 });
    	 if(self.maxclass != undefined){
    		//-----------增加放大效果-----------------------------------
			 self.input.observe('keyup',function(){//失失焦点时验证其值
				 var ispassword = $(self.id+'_chbut');
				 if(ispassword== undefined){//不是密码输入框
					 self.showMax(this.value);
				 }else if(self.isshow){//是密码框，必须是在显示密码状态下放大效果才起作用
					 self.showMax(this.value);
				 }
     	 	});
			 //-----------隐藏-----------------------------------------
		 	document.observe('mouseout',function(event){
		   	 	if(self.div.visible){//图标可见
		   	 		if(self.maxDiv!=undefined){
			 			self.maxDiv.hide();
		   	 		}
			 	}
	   	 	});
    	 }
    	
    },
    chVeris : function(va){
    	var self = this;
    	if((va.trim().length<1) && !self.allownull){//不允许为空
			 self.ico.show();
			 self.veriSuccess=false;
			 self.ajaxchtype = false;//为false表示验证结束
		 }else if(va.trim().length>0){//有输入内容
				 if(''!=self.verification){
					 var chend = false;//验证结果
	   			 if('js'==self.chtype)
	   				 chend=self.chInputJs(va);
	   			 else if('regex'==self.chtype){
	   				 chend=self.chInputRegex(va);
	   			 }else if('server'==self.chtype){
	   				self.ajaxchtype = true;// 是否正在执行ajva验证
	   				self.chInputServer(va);
	   				chend = true;
	   			 }else if('ext'==self.chtype){//扩展
	   				 chend=self.chInputJs($(self.id));
	   			 }
	   			 if(!chend){
	   				 self.ico.show();
	   				 self.veriSuccess=false;
	   			 }else{
	   				 self.ico.hide();
	   				 self.veriSuccess=true;
	   			 }
			 }else{
				 self.ico.hide();
				 self.veriSuccess=true;
			 }
		 }else{//没有输入内容，可以为空
			 self.ico.hide();
			 self.veriSuccess=true;
		 }
    },
    //--------regex------------------------
    chInputRegex : function(value){
    	var issc = true;
    	var regu = "^" + this.verification + "$";
    	var re = new RegExp(regu);
    	return re.test(value);
    },
    //---------自定义js验证-----------------
    chInputJs : function(value){
    	return this.verification(value);
    },
    //---------服务端验证-------------------
    chInputServer : function(value){
    	var self = this;
    	var idx = 0;//
    	var parsv=[];
    	parsv[idx++]='fun='+self.verification;
    	parsv[idx++]='&thisvalue='+value;
    	if(self.pva!=undefined){
    		try{
	    		for(var c=0;c<self.pva.length;c++){
	    			parsv[idx++]='&'+self.pva[c]+'='+$(self.pva[c]).value;
	    		}
    		}catch(E){}
    	}
    	new Ajax.Request(self.rurl+'/nokatag/ajax/inputtextcheck'+self.id+'.itck', {
    		method:'post',
            parameters: parsv.join(''),
            onSuccess: function(response) {
            	var v = response.responseText;
            	if('1'==v){//成功
            		self.ico.hide();
            		self.veriSuccess = true;
            	}else{//失败
            		self.ico.show();
            		self.veriSuccess=false;
            	}
            	self.ajaxchtype = false;//验证完成
            },
            onFailure : function(transport) {
            	self.ajaxchtype = false;//验证完成
            }
    	});
    },
    //-------------空验证------------------
    chInputNull : function(value){
    	if (''==value)
    		return true;
    	var regu = "^[ ]+$";
    	var re = new RegExp(regu);
    	return re.test(value);
    },
    //--------消息提示显示、隐藏------------
    msgShowHide : function(){
    	var self = this;
   	 	self.ico.observe('mousemove',function(){//失失焦点时验证其值
   	 		if(self.ico.visible){//图标可见
   	 			var ptop =self.ico.positionedOffset().top+self.ico.offsetHeight;
   	 			var pleft=self.ico.positionedOffset().left+self.ico.offsetWidth;
   	 			self.div.absolutize();
   	 			self.div.style.top=ptop+'px';//
   	 			self.div.style.left=pleft+'px';
   	 			self.div.setStyle({top:ptop+'px',left:pleft+'px'});
   	 			self.div.show();
   	 		}
   	 	});
   	 	self.ico.observe('mouseout',function(event){
	   	 	if(self.div.visible){//图标可见
		 			self.div.hide();
		 	}
   	 	});
    },
    onChangeColor : function(){
    	var self = this;
    	var obj = $(self.id+'_sdDiv');
    	obj.observe('mousemove',function(){
     		self.setSelectStyle(obj);
     	});
    	
         obj.observe('mouseout',function(event){
         	var element = event.element();
         	setTimeout(function(){
                 	self.setNoSelectStyle(obj);
             }, 500);
         });
    },
    //----------选种状态-----------------------
    setSelectStyle : function(obj){
    	obj.setStyle({borderColor:'#5794bf'});
    },
    //-----------未选种状态-------------------
    setNoSelectStyle : function(obj){
    	obj.setStyle({borderColor:'#AFAFAF'});
    },
   //----------------格式化时间--------------------------------------------------
    formdates : function(date,fmt){
    	if(undefined == date || date.trim().length<1 || date.hTrim().length<1){
            return '';
        }
    	if(undefined == fmt || fmt.trim().length<1){
    		return date;
    	}
    	try{
    		var sc = date.replace(/-/g, "/");
    		sc = sc.substring(0,sc.indexOf('.'));
    		date = new Date(Date.parse(sc));
    		return date.Format(fmt);
    	}catch(e){}
    	return date;
    }
});